package com.zbkj.front.controller;

import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.response.DeliveryPersonnelLoginResponse;
import com.zbkj.common.response.OrderFrontDataResponse;
import com.zbkj.common.response.RefundOrderResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.front.service.FrontOrderService;
import com.zbkj.service.service.DeliveryPersonnelService;
import com.zbkj.service.service.RefundOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 配送员表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/front/delivery/personnel")
@Api(tags = "配送员")
public class DeliveryPersonnelController {

    @Autowired
    private DeliveryPersonnelService deliveryPersonnelService;
    @Autowired
    private FrontOrderService frontOrderService;
    @Autowired
    private RefundOrderService refundOrderService;


    @ApiOperation(value = "配送员登录(门店登录)")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public CommonResult<DeliveryPersonnelLoginResponse> login(@RequestBody @Validated DeliveryPersonnelLoginRequest request) {
        return CommonResult.success(deliveryPersonnelService.login(request));
    }


    @ApiOperation(value = "配送员订单管理")
    @RequestMapping(value = "/order/manage", method = RequestMethod.POST)
    public CommonResult<CommonPage<OrderFrontDataResponse>> orderManage(@RequestBody @Validated DeliveryPersonnelOrderStatusRequest deliveryPersonnelOrderStatusRequest) {
        return CommonResult.success(CommonPage.restPage(deliveryPersonnelService.orderManage(deliveryPersonnelOrderStatusRequest)));
    }


    @ApiOperation(value = "查询配送员是否有待配送订单")
    @RequestMapping(value = "/to/delivered/{status}", method = RequestMethod.GET)
    public CommonResult<Boolean> toDelivered(@PathVariable(value = "status") Integer status) {
        return CommonResult.success(deliveryPersonnelService.toDelivered(status));
    }


    @ApiOperation(value = "配送员扫门店订单二维码-配送、上门退货退款")
    @RequestMapping(value = "/scan/code", method = RequestMethod.POST)
    public CommonResult<Boolean> scanCode(@RequestBody @Valid OrderNoRequest orderNoRequest) {
        return CommonResult.success(deliveryPersonnelService.scanCode(orderNoRequest.getOrderNo()));
    }


    @ApiOperation(value = "配送员已送达")
    @RequestMapping(value = "/delivered/{orderNo}", method = RequestMethod.GET)
    public CommonResult<Boolean> delivered(@PathVariable(value = "orderNo") String orderNo) {
        return CommonResult.success(deliveryPersonnelService.delivered(orderNo));
    }


    @ApiOperation(value = "调整订单")
    @RequestMapping(value = "/adjust/order", method = RequestMethod.POST)
    public CommonResult<Boolean> adjustOrder(@RequestBody @Valid ProductAttrAdjustRequest productAttrAdjustRequest) {
        return CommonResult.success(deliveryPersonnelService.adjustOrder(productAttrAdjustRequest));
    }


    @ApiOperation(value = "发送定位")
    @RequestMapping(value = "/send/location", method = RequestMethod.POST)
    public CommonResult<Boolean> sendLocation(@RequestBody @Valid LocationRequest locationRequest) {
        return CommonResult.success(deliveryPersonnelService.sendLocation(locationRequest));
    }


    @ApiOperation(value = "配送员退款订单列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<RefundOrderResponse>> refundOrderList(@ModelAttribute @Valid RefundOrderDeliveryPersonnelRequest refundOrderDeliveryPersonnelRequest) {
        return CommonResult.success(CommonPage.restPage(frontOrderService.getRefundOrderListByDeliveryPersonnel(refundOrderDeliveryPersonnelRequest)));
    }


    @Deprecated
    @ApiOperation(value = "配送员扫码-上门退货退款")
    @RequestMapping(value = "/scan/return/goods/{refundOrderNo}", method = RequestMethod.GET)
    public CommonResult<Boolean> scanCodeReturnGoods(@PathVariable(value = "refundOrderNo") String refundOrderNo) {
        return CommonResult.success(refundOrderService.scanCodeReturnGoods(refundOrderNo));
    }


    @ApiOperation(value = "配送员确定上门")
    @RequestMapping(value = "/confirm/{refundOrderNo}", method = RequestMethod.GET)
    public CommonResult<Boolean> confirm(@PathVariable(value = "refundOrderNo") String refundOrderNo) {
        return CommonResult.success(refundOrderService.confirm(refundOrderNo));
    }

}



